import { analysisIntent, IntentHandler } from "../core/tool/baseScene";
import { mathType } from "../core/tool/face";
import component from "../core/tool/component";
import { ButtonIntentConfig, Icard } from "../core/tool/Icard";
import { PopupService, closeType } from "../service/PopupService";
import { ModernPopupService } from "../service/ModernPopupService";

/**
 * 弹窗组件 - 主要负责UI交互和状态管理
 * 职责：
 * 1. 管理弹窗显示状态
 * 2. 处理用户交互
 * 3. 管理意图处理器
 */
export default class pop_component extends component {
    private _intentHandlers: Map<string, IntentHandler> = new Map();
    _content: string = '';
    _lisen?: Function;
    _show: boolean = false;
    
    // 使用依赖注入获取现代化服务
    private _modernPopupService = this.getService<ModernPopupService>('ModernPopupService');
    
    constructor() {
        super();
    }
    
    /**
     * 检查弹窗是否显示
     * @returns 是否显示
     */
    haveShow(): boolean {
        return this._show;
    }
    
    /**
     * 获取排序后的意图处理器
     * @returns 排序后的意图处理器数组
     */
    getIntentHandlers(): IntentHandler[] {
        const handlers = [...this._intentHandlers.values()];
        return this._modernPopupService.sortIntentHandlers(handlers);
    }
    
    /**
     * 监听用户输入
     * @param cb 回调函数
     * @param self 执行上下文
     */
    lisen(cb: Function, self?: any) {
        this.reg(mathType.全部拦截, '', cb, self);
    }
    
    /**
     * 激活弹窗
     */
    active(): void {
        this._show = true;
        const formattedContent = this._modernPopupService.formatPopupContent(this._content);
        this.client_text(formattedContent);
    }
    
    /**
     * 注册意图处理器
     * @param mathType 匹配类型
     * @param mathStr 匹配字符串
     * @param action 动作函数
     * @param self 执行上下文
     * @param isHideMath 是否隐藏匹配
     */
    reg(mathType: mathType, mathStr: string, action: Function, self: any, isHideMath: boolean = false): void {
        const key = this._modernPopupService.generateIntentKey(mathType, mathStr);
        const handler = this._modernPopupService.createIntentHandler(mathType, mathStr, action, self, isHideMath);
        this._intentHandlers.set(key, handler);
    }
    
    /**
     * 注册弹窗按钮意图
     * @param buttonConfigs 按钮配置数组
     * @param player 玩家实例
     * @param card 卡片对象
     */
    registerPopupButtonIntents(buttonConfigs: ButtonIntentConfig[], player: any, card: Icard): void {
        buttonConfigs.forEach(config => {
            card.button(config.text);
        });
        
        const success = this._modernPopupService.registerButtonIntents(buttonConfigs, player, this._intentHandlers);
        if (!success) {
            console.warn('部分按钮意图注册失败');
        }
    }
    
    /**
     * 关闭弹窗
     */
    close(): void {
        const resetState = this._modernPopupService.resetPopupState();
        this._lisen = resetState.listener;
        this._content = resetState.content;
        this._show = resetState.isShow;
        
        this._modernPopupService.clearIntentHandlers(this._intentHandlers);
    }
    
    /**
     * 获取弹窗状态
     * @returns 弹窗状态信息
     */
    getPopupStatus(): any {
        return this._modernPopupService.getPopupStatus(this._show, this._content, this._intentHandlers.size);
    }
    
    /**
     * 设置弹窗内容
     * @param content 内容
     */
    setContent(content: string): void {
        this._content = this._modernPopupService.formatPopupContent(content);
    }
    
    /**
     * 验证弹窗状态
     * @returns 验证结果
     */
    isValid(): boolean {
        return this._modernPopupService.validatePopupState(this._show, this._content);
    }
}

